/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package homomorphism.list;

import java.io.IOException;
import java.util.Iterator;

/**
 *
 * @author liuyu
 * This is a efficient version of BasicListHomomorphismInterface
 */
public abstract class ListHomomorphism<T1, T2> implements BasicListHomomorphismInterface<T1, T2> {

    public abstract T2 filter(T1 val);

    public abstract T2 plus(T2 a, T2 b);

    public T2 aggregator(Iterator<T2> values) throws IOException {
        T2 rst = null;
        int count = 0;
        while (values.hasNext()) {
            T2 tmp = values.next();
            if (count == 0) {
                rst = tmp;
            } else {
                rst = plus(rst, tmp);
            }
            count++;
        }

        if (rst == null) {
            throw new NullPointerException("The plus return null!");
        }
        return rst;
    }

    public Object GetValue(T2 val) {
        return val;
    }
}
